#include "mytcpsockettouser.h"
#include <QJsonObject>
#include <QJsonDocument>
#include <QDateTime>
#include <QJsonArray>
#include "sqlmain.h"

MyTcpSocketToUser::MyTcpSocketToUser(QObject *parent)
    : MyTcpSocket{parent}
{

}

void MyTcpSocketToUser::handleFrame(const QJsonObject &obj)
{
    int type = obj.value("type").toInt();
    if(type == 2001)
        handleControlFrame(obj);    //控制语句
    else if(type == 3001)
        handleLoginFrame(obj);
    else if(type == 3002)
        handleRegisterFrame(obj);
    else if(type == 3011)
        handleDeviceValue(obj);
    else if(type == 3012)
        handleDeviceListFrame(obj);
    else if(type == 3013)
        handleDeviceHistoryFrame(obj);
    else if(type == 3014)
        handleDeviceTypeFrame(obj);
    else if(type == 3015)
        handleAddDeviceFrame(obj);
    else if(type == 3016)
        handleDelDeviceFrame(obj);
}

void MyTcpSocketToUser::handleControlFrame(const QJsonObject &obj)
{
    emit dataSendSig(QJsonDocument(obj).toJson());
    int deviceTypeId = obj.value("device_type_id").toInt();
    int deviceTypeIndex = obj.value("device_type_index").toInt();
    if(deviceTypeId == 301 || deviceTypeId == 302 || deviceTypeId == 306)
    {
        bool cmd = obj.value("cmd").toBool();
        SqlMain::getObject()->insertValue(deviceTypeId, deviceTypeIndex, cmd);
    }
    else if(deviceTypeId == 303 || deviceTypeId == 305)
    {
        int cmd = obj.value("cmd").toInt();
        SqlMain::getObject()->insertValue(deviceTypeId, deviceTypeIndex, cmd);
    }
    else if(deviceTypeId == 307)
    {
        QString cmd = obj.value("cmd").toString();
        SqlMain::getObject()->insertValue(deviceTypeId, deviceTypeIndex, cmd);
    }
    else
    {
        QString cmd = QJsonDocument(obj.value("cmd").toObject()).toJson();
        SqlMain::getObject()->insertValue(deviceTypeId, deviceTypeIndex, cmd);
    }
}

void MyTcpSocketToUser::handleLoginFrame(const QJsonObject &obj)
{
    QString username = obj.value("username").toString();
    QString password = obj.value("password").toString();
    bool ok = SqlMain::getObject()->userLogin(username, password);
    QJsonObject reObj;
    reObj.insert("type", 4001);
    reObj.insert("username", username);
    reObj.insert("re", ok);
    this->write(QJsonDocument(reObj).toJson());
}

void MyTcpSocketToUser::handleRegisterFrame(const QJsonObject &obj)
{
    QString username = obj.value("username").toString();
    QString password = obj.value("password").toString();
    bool ok = SqlMain::getObject()->userRegister(username, password);
    QJsonObject reObj;
    reObj.insert("type", 4002);
    reObj.insert("username", username);
    reObj.insert("re", ok);
    if(!ok)
        reObj.insert("re_info", "用户名或密码错误");
    else
        reObj.insert("re_info", "成功");
    this->write(QJsonDocument(reObj).toJson());
}

void MyTcpSocketToUser::handleDeviceValue(const QJsonObject &obj)
{
    int deviceTypeId = obj.value("device_type_id").toInt();
    int deviceTypeIndex = obj.value("device_type_index").toInt();
    QJsonObject reObj = obj;
    reObj.insert("type", 4011);
    if(deviceTypeId <= 104 && deviceTypeId >= 101)
    {
        //double
        reObj.insert("value",
                    SqlMain::getObject()->getDoubleValue(
                         deviceTypeId,
                         deviceTypeIndex));
    }
    else if(deviceTypeId >= 105 && deviceTypeId <= 109)
    {
        //int
        reObj.insert("value",
                    SqlMain::getObject()->getIntValue(
                         deviceTypeId,
                         deviceTypeIndex));
    }
    else if(deviceTypeId >= 201 && deviceTypeId <=208)
    {
        //bool
        reObj.insert("value",
                    SqlMain::getObject()->getBoolValue(
                         deviceTypeId,
                         deviceTypeIndex));
    }
    this->write(QJsonDocument(reObj).toJson());
}

void MyTcpSocketToUser::handleDeviceListFrame(const QJsonObject &obj)
{
    QJsonObject reObj;
    reObj.insert("type", 4012);
    reObj.insert("list", SqlMain::getObject()->getDeviceList());
    this->write(QJsonDocument(reObj).toJson());
}

void MyTcpSocketToUser::handleDeviceHistoryFrame(const QJsonObject &obj)
{
    int deviceTypeId = obj.value("device_type_id").toInt();
    int deviceTypeIndex = obj.value("device_type_index").toInt();
    int mode = obj.value("mode").toInt();

    QJsonObject reObj = obj;
    reObj.insert("type", 4013);
    reObj.insert("list", SqlMain::getObject()->getDeviceHistory(deviceTypeId,
                                                                deviceTypeIndex,
                                                                mode));
    this->write(QJsonDocument(reObj).toJson());

}

void MyTcpSocketToUser::handleDeviceTypeFrame(const QJsonObject &obj)
{
    QJsonObject reObj;
    reObj.insert("type", 4014);
    reObj.insert("list", SqlMain::getObject()->getDeviceType());
    this->write(QJsonDocument(reObj).toJson());
}

void MyTcpSocketToUser::handleAddDeviceFrame(const QJsonObject &obj)
{
    int deviceTypeId = obj.value("device_type_id").toInt();
    int deviceTypeIndex = obj.value("device_type_index").toInt();
    QString name = obj.value("name").toString();
    QString unit = obj.value("unit").toString();
    QString remarks = obj.value("remarks").toString();

    QJsonObject reObj = obj;
    reObj.insert("type", 4015);
    reObj.insert("re", SqlMain::getObject()->addDevice(name, deviceTypeId,
                                                       deviceTypeIndex, unit,
                                                       remarks));
    this->write(QJsonDocument(reObj).toJson());
}

void MyTcpSocketToUser::handleDelDeviceFrame(const QJsonObject &obj)
{
    int deviceTypeId = obj.value("device_type_id").toInt();
    int deviceTypeIndex = obj.value("device_type_index").toInt();
    QJsonObject reObj = obj;
    reObj.insert("type", 4016);
    reObj.insert("re", SqlMain::getObject()->delDevice(deviceTypeId,
                                                       deviceTypeIndex));
    this->write(QJsonDocument(reObj).toJson());
}







